﻿Public Class FormMain
    Private WithEvents UcDigitalKeyboard1 As ucDigitalKeyboard
    Dim objUser As New clsUser
    Dim objJlMst As New clsJlMst
    Dim objJpMst As New clsJpMst
    Dim objJp As New clsJp
    Dim objJPck As New clsJPck
    Dim objPgdMes As New clsPgdMes
    Private Sub FormMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        connectDB()
        getUserListByCJ()
        initCmbModel()
        setDgvMesData()
    End Sub

    Private Sub connectDB()
        g_szInitFileName = My.Application.Info.DirectoryPath & "\Client_XJ.ini"
        g_jsInitFileName = My.Application.Info.DirectoryPath & "\js.ini"
        g_Server.szDrive = GetIniFile("SERVER", "DRIVE", "PostgreSQL ANSI", g_szInitFileName)
        g_Server.szIPadr = GetIniFile("SERVER", "IP", "", g_szInitFileName)
        g_Server.szDbn = GetIniFile("SERVER", "DBN", "ZHENANSF", g_szInitFileName)
        g_Server.szUid = GetIniFile("SERVER", "UID", "sa", g_szInitFileName)
        g_Server.szPas = GetIniFile("SERVER", "PAS", "sa", g_szInitFileName)
        g_Server.iOver = CInt(GetIniFile("SERVER", "OVER", "-1", g_szInitFileName))

        If False = g_fDBconnect And 0 = g_iDBconnResult Then
            If "" = g_Server.szIPadr Or "0.0.0.0" = g_Server.szIPadr Then
            Else
                g_fDBconnect = True

                'データベース接続
                With g_Server
                    g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    If 1 <> g_iDBconnResult Then
                        .szDrive = CStr(IIf("PostgreSQL" = .szDrive, "PostgreSQL ANSI", "PostgreSQL"))
                        g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    End If
                End With
            End If
        End If
    End Sub

    Private Sub getUserListByCJ()
        Dim dtUser As DataTable = objUser.getUserListByCJ(2)
        cmbOpers.Properties.Items.Clear()
        For index = 0 To dtUser.Rows.Count - 1
            cmbOpers.Properties.Items.Add(dtUser.Rows(index)("user_name"))
        Next
        cmbOpers.Properties.Items(0).CheckState = CheckState.Checked
        cmbOpers.Properties.DropDownRows = cmbOpers.Properties.Items.Count + 1
        cmbOpers.Properties.ShowButtons = False
        cmbOpers.Properties.ShowPopupCloseButton = False
    End Sub

    Private Sub initCmbModel()
        Dim dt As DataTable = objJlMst.getList()
        Dim drNew As DataRow = dt.NewRow()
        drNew("mst_value") = String.Empty
        dt.Rows.InsertAt(drNew, 0)
        cmbModel.DisplayMember = "mst_value"
        cmbModel.ValueMember = "mst_value"
        cmbModel.DataSource = dt
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        setDgvMesData()
    End Sub

    Private Sub setDgvMesData()
        Dim dt As DataTable = objJpMst.selectAll(cmbType.Text.Trim(), cmbModel.Text.Trim(), cmbSpec.Text.Trim())
        dt.Columns.Add("validity_name")
        If dt.Rows.Count > 0 Then
            For Each row As DataRow In dt.Rows
                If row("validity") = "0" Then
                    row("validity_name") = "无效"
                Else
                    row("validity_name") = "有效"
                End If
            Next
        End If
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dt
        getProject()
    End Sub

    Private Sub getProject()
        If dgv.SelectedRows.Count > 0 Then
            Dim material_cod As String = dgv.SelectedRows(0).Cells("col_material_code").Value.ToString()
            Dim dtPMD As DataTable = objPgdMes.getPgdMesDetailsListByMaterialCode(material_cod)
            cmbMesProject.ValueMember = "mes_project_code"
            cmbMesProject.DisplayMember = "sys_id"
            cmbMesProject.DataSource = dtPMD
        End If
    End Sub

    Private Sub dgv_SelectionChanged(sender As Object, e As EventArgs) Handles dgv.SelectionChanged
        getProject()
    End Sub

    Private Sub btn_Save_Click(sender As Object, e As EventArgs) Handles btn_Save.Click
        If dgv.SelectedRows.Count > 0 Then
            If String.IsNullOrEmpty(cmbMesProject.Text.Trim()) = False Then
                If txt_finish_count_JP.Value > 0 Then

                    Dim material_cod As String = dgv.SelectedRows(0).Cells("col_material_code").Value.ToString()
                    Dim dttxxx As DataTable = objPgdMes.getProjectListB(cmbMesProject.SelectedValue.ToString(), material_cod)
                    Dim bg_me_all As Integer = Integer.Parse(dttxxx.Rows(0)("finish_count_bg_me").ToString())
                    Dim op_count As Integer = Integer.Parse(dttxxx.Rows(0)("oper_count").ToString())
                    If txt_finish_count_JP.Visible + op_count <= bg_me_all Then
                        Dim SysId As String = dgv.SelectedRows(0).Cells("col_sys_id").Value.ToString()
                        objJp.insert("ok", material_cod, txt_finish_count_JP.Value.ToString(), cmbOpers.Text.Trim(), "0", cmbMesProject.SelectedValue.ToString())
                        'objPgdMesWorkReport.update("ok", SysId, txt_finish_count_JP.Text.Trim(), g_Current_UserName)
                        Dim typp As String = dgv.SelectedRows(0).Cells("Col_type").Value.ToString()
                        Dim rtn As Integer
                        If typp.Contains("保护胶") Then
                            rtn = objJPck.insert(String.Empty, "1", material_cod, txt_finish_count_JP.Value.ToString(), String.Empty, cmbOpers.Text.Trim(), "1")
                        Else
                            rtn = objJPck.insert(String.Empty, "0", material_cod, txt_finish_count_JP.Value.ToString(), String.Empty, cmbOpers.Text.Trim(), "1")
                        End If
                        If rtn <> -2 Then
                            MsgBox("保存成功")
                        Else
                            MsgBox("保存失败")
                        End If
                    Else
                        MsgBox("报工数量大于项目所需总数！剩余所需片数为：" & (op_count - bg_me_all).ToString())
                    End If
                Else
                    MsgBox("完工数量需 > 0")
                End If
            Else
                MsgBox("请选择报工项目！")
            End If
        Else
            MsgBox("请选择生产计划！")
        End If
    End Sub

    Private Sub btnJLJC_Click(sender As Object, e As EventArgs) Handles btnJLJC.Click
        Dim formJLJC As New FormJLJC()
        formJLJC.usr = cmbOpers.Text.Trim()
        formJLJC.ShowDialog(Me)
    End Sub

    Private Sub btnJLJCRecord_Click(sender As Object, e As EventArgs) Handles btnJLJCRecord.Click
        Dim formJLJCRecord As New FormJLJCRecord()
        formJLJCRecord.usr = cmbOpers.Text.Trim()
        formJLJCRecord.ShowDialog(Me)
    End Sub

    Private Sub btnWorkReportList_Click(sender As Object, e As EventArgs) Handles btnWorkReportList.Click
        Dim formWorkReportList As New FormWorkReportList()
        formWorkReportList.ShowDialog(Me)
    End Sub

    Private Sub btnMesWorkReport_Click(sender As Object, e As EventArgs) Handles btnMesWorkReport.Click
        Dim formMesWorkReport As New FormMesWorkReport()
        formMesWorkReport.usr = cmbOpers.Text.Trim()
        formMesWorkReport.ShowDialog(Me)
    End Sub

    Private Sub btnMesWorkReportList_Click(sender As Object, e As EventArgs) Handles btnMesWorkReportList.Click
        Dim formMesWorkReportList As New FormMesWorkReportList()
        formMesWorkReportList.ShowDialog(Me)
    End Sub

    Private Sub txt_MouseDown(sender As Object, e As MouseEventArgs) Handles txt_finish_count_JP.MouseDown
        UcDigitalKeyboard1 = New ucDigitalKeyboard
        If sender.MaskBox.Mask.EditMask = "d" Then
            UcDigitalKeyboard1.B_01 = False
        Else
            UcDigitalKeyboard1.B_01 = True
        End If

        If sender.GetType().Name.Equals("TextEdit") Then
            UcDigitalKeyboard1.B_02 = True
        Else
            UcDigitalKeyboard1.B_02 = False
        End If

        UcDigitalKeyboard1.Txt = sender

        Me.Controls.Add(UcDigitalKeyboard1)
        UcDigitalKeyboard1.Show()
        UcDigitalKeyboard1.BringToFront()
        UcDigitalKeyboard1.Focus()
    End Sub

    Private Sub FormMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        DBacs.Disconnect()
    End Sub

End Class

Public Class entityPgdMes
    Public pgd_code As String
    Public oper_date As String
    Public mes_project_code As String
    Public mes_project_name As String
    Public support_model As String
    Public type_value As String
    Public oper_count As String
    Public lhj As String
    Public lhj_type As String
    Public note As String
    Public workshop_id As Integer
    Public validity As String
    Public status As String
    Public origin As String
    Public Ope As String
    Public start_time As String
    Public end_time As String
    Public jp_type As String
    Public jp_spec As String
End Class
